package com.jcl.emessage.core.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import java.util.Properties;

/**
 * @ClassName EmailConfig
 * @Description TODO
 * @Author yyliu
 * @Date 2018/11/12 10:08
 * @Version 1.0
 **/
@Configuration
@PropertySource(value = "classpath:email.properties")
public class EmailConfig {

    private static final Logger logger = LoggerFactory.getLogger(EmailConfig.class);

    @Value("${spring.mail.host}")
    private String  host;
    @Value("${spring.mail.username}")
    private String  username;
    @Value("${spring.mail.password}")
    private String  password;
    @Value("${spring.mail.port}")
    private Integer  port;
    @Value("${spring.mail.defaultEncoding}")
    private String encoding;

    @Value("${spring.mail.properties.mail.transport.protocol}")
    private String protocol;
    @Value("${spring.mail.properties.mail.smtp.auth}")
    private Boolean auth;
    @Value("${spring.mail.properties.mail.smtp.starttls.enable}")
    private Boolean starttlsEnable;
    @Value("${spring.mail.properties.mail.smtp.starttls.required}")
    private Boolean starttlsRequired;
    @Value("${spring.mail.properties.mail.debug}")
    private Boolean mailDubeg ;
    @Value("${spring.mail.properties.mail.smtp.socketFactory.class}")
    private String socketFactoryClass;
    @Value("${spring.mail.properties.mail.smtp.socketFactory.port}")
    private Integer smtpSocketFactoryPort ;

    @Value("${email.fromMail.addr}")
    private String fromAddr;
    @Value("${email.tryTime}")
    private Integer tryTime;

    @Bean
    public JavaMailSender mailSender(){
        logger.info("email init setting start ...");
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost(host);
        mailSender.setUsername(username);
        mailSender.setPassword(password);
        mailSender.setDefaultEncoding(encoding);
        //mailSender.setPort(port);

        Properties props = mailSender.getJavaMailProperties();
        // props.put("mail.transport.protocol", protocol);
        // 登录服务器是否需要认证
        props.put("mail.smtp.auth", auth);
        // props.put("mail.smtp.starttls.enable", starttlsEnable);
        // props.put("mail.smtp.starttls.required", starttlsRequired);
        // props.put("mail.debug", mailDubeg);
        // SSL证书Socket工厂
        props.put("mail.smtp.socketFactory.class", socketFactoryClass);
        // 使用SMTPS协议465端口
        props.put("mail.smtp.socketFactory.port", smtpSocketFactoryPort);

        logger.info("email init setting end .");

        return mailSender;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }

    public String getEncoding() {
        return encoding;
    }

    public void setEncoding(String encoding) {
        this.encoding = encoding;
    }

    public String getProtocol() {
        return protocol;
    }

    public void setProtocol(String protocol) {
        this.protocol = protocol;
    }

    public Boolean getAuth() {
        return auth;
    }

    public void setAuth(Boolean auth) {
        this.auth = auth;
    }

    public Boolean getStarttlsEnable() {
        return starttlsEnable;
    }

    public void setStarttlsEnable(Boolean starttlsEnable) {
        this.starttlsEnable = starttlsEnable;
    }

    public Boolean getStarttlsRequired() {
        return starttlsRequired;
    }

    public void setStarttlsRequired(Boolean starttlsRequired) {
        this.starttlsRequired = starttlsRequired;
    }

    public Boolean getMailDubeg() {
        return mailDubeg;
    }

    public void setMailDubeg(Boolean mailDubeg) {
        this.mailDubeg = mailDubeg;
    }

    public String getSocketFactoryClass() {
        return socketFactoryClass;
    }

    public void setSocketFactoryClass(String socketFactoryClass) {
        this.socketFactoryClass = socketFactoryClass;
    }

    public Integer getSmtpSocketFactoryPort() {
        return smtpSocketFactoryPort;
    }

    public void setSmtpSocketFactoryPort(Integer smtpSocketFactoryPort) {
        this.smtpSocketFactoryPort = smtpSocketFactoryPort;
    }

    public String getFromAddr() {
        return fromAddr;
    }

    public void setFromAddr(String fromAddr) {
        this.fromAddr = fromAddr;
    }

    public Integer getTryTime() {
        return tryTime;
    }

    public void setTryTime(Integer tryTime) {
        this.tryTime = tryTime;
    }
}
